package com.goldcn.goods.service;

import com.goldcn.common.CommonTools;
import com.goldcn.common.util.IdGen;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.goods.dao.GoodsClassifyCommissionMapper;
import com.goldcn.goods.dao.GoodsClassifyRebateMapper;
import com.goldcn.goods.dto.ClazzImageDto;
import com.goldcn.goods.dto.ClazzValDto;
import com.goldcn.goods.model.GoodsClassifyCommission;
import com.goldcn.goods.model.GoodsClassifyRebate;
import com.goldcn.goods.vo.ClazzImageVo;
import com.goldcn.goods.vo.ClazzRateVo;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.order.service.RebateService;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/11/14.
 */
@Service
public class GoodsClassifyCommissionService {

    @Autowired
    private GoodsClassifyCommissionMapper goodsClassifyCommissionMapper;

    @Autowired
    private GoodsClassifyRebateMapper rebateMapper;

    @Autowired
    RebateService rebateService;

    /*
     * get val by clazzID
     *  根据clazzID得到val
     * @param goodsLabel
     * @return
     * */
    @Transactional
    public Integer getGoodsClassifyValByClazzID(String clazzID) {

        GoodsClassifyCommission goodsClassifyCommission = goodsClassifyCommissionMapper.getValByClassID(clazzID, SystemContext.getCompanyID());
        if (goodsClassifyCommission == null) {
            return 0;
        } else {
            return goodsClassifyCommission.getVal();
        }
    }

    /**
     * after edit,to insert or update
     * 通过传入的clazzID和val 修改或者插入对应的一条数据
     *
     * @return
     */
    @Transactional
    public Integer operateClassifyValByClazzID(String clazzID, Integer val) {
        GoodsClassifyCommission goodsClassifyCommission = goodsClassifyCommissionMapper.selectByClazzID(clazzID, SystemContext.getCompanyID());
        Integer result = 0;
        if (goodsClassifyCommission == null) {
            //表示这里要执行插入语句
            goodsClassifyCommission = new GoodsClassifyCommission(clazzID, IdGen.get().nextId());
            goodsClassifyCommission.setVal(val);
            goodsClassifyCommission.setCompanyID(SystemContext.getCompanyID());
            result = goodsClassifyCommissionMapper.insertSelective(goodsClassifyCommission);
           /* if(result != 1){
                throw CommonTools.createException(ErrorCodeEnum.updateCommissionFailure);
            }*/
        } else {
            //这里需要更新语句  只需要修改val的值即可 需要传入一个对象
            goodsClassifyCommission.setClazzID(clazzID);
            goodsClassifyCommission.setVal(val);
            result = goodsClassifyCommissionMapper.updateByClazzID(goodsClassifyCommission);
            /*if(result != 1){
                //这里表示修改失败 则要抛出异常
                throw CommonTools.createException(ErrorCodeEnum.updateCommissionFailure);
            }*/
        }
        return result;
    }

    /**
     * 通过传入的clazzID和插入对应的一条数据
     *
     * @return
     */
    @Transactional
    public Object insertClassifyValByClazzID(String clazzID) {
        Integer result = 0;
        //表示这里要执行插入语句
        GoodsClassifyCommission goodsClassifyCommission = new GoodsClassifyCommission(clazzID, IdGen.get().nextId());
        goodsClassifyCommission.setDiscountRate("{'oneLevel':100,'twoLevel':100,'threeLevel':100}");
        goodsClassifyCommission.setCompanyID(SystemContext.getCompanyID());
        result = goodsClassifyCommissionMapper.insertSelective(goodsClassifyCommission);
        if (result != 1) {
            throw CommonTools.createException(ErrorCodeEnum.updateCommissionFailure);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(goodsClassifyCommission.getClazzID(), goodsClassifyCommission);

        return resultMap;
    }


    /**
     * 可能涉及到别的扣点操作 通过最后的节点得到
     *
     * @param clazzIDOne
     * @param clazzIDTwo
     * @return
     */
    @Transactional
    public Integer getRateByClazzID(String clazzIDOne, String clazzIDTwo) {
        GoodsClassifyCommission goodsClassifyCommission = null;
        Long companyID = SystemContext.getCompanyID();
        if (clazzIDTwo != null && !("").equals(clazzIDTwo)) {
            GoodsClassifyCommission gcc = goodsClassifyCommissionMapper.getValByClassID(clazzIDTwo, companyID);
            if (gcc == null) {
                goodsClassifyCommission = goodsClassifyCommissionMapper.getValByClassID(clazzIDOne, companyID);
                ;
            } else {
                if (gcc.getVal() == 0) { //如果二级分类扣点为零，则取一级分类的扣点
                    goodsClassifyCommission = goodsClassifyCommissionMapper.getValByClassID(clazzIDOne, companyID);
                } else {
                    goodsClassifyCommission = gcc;
                }
            }
        } else {
            goodsClassifyCommission = goodsClassifyCommissionMapper.getValByClassID(clazzIDOne, companyID);
        }
        if (goodsClassifyCommission == null) {
            return 0;
        } else {
            return goodsClassifyCommission.getVal();
        }

    }

    /**
     * TODO
     * 新增员工回扣，add by wanglq
     *
     * @param clazzIDOne
     * @param clazzIDTwo
     * @return bankVal, employeeVal
     */
    @Transactional
    public Map<String, Integer> getRateByClazzID2(String clazzIDOne, String clazzIDTwo, Long goodsID, Long sellerID) {
        return rebateService.getRebateVal(clazzIDOne, clazzIDTwo, goodsID, sellerID);
    }

    /**
     * 查询扣点值通过分类ID集合
     *
     * @param clazzIDs
     * @return
     */
    public Object selectByClazzIDList(List<String> clazzIDs) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("list", clazzIDs);
        //map.put("companyID",SystemContext.getCompanyID());
        map.put("companyID", SystemContext.getCompanyID());//通过分类ID查询不需要公司ID，云管控传递，也获取不到
        List<ClazzValDto> clazzValDtoList =
                goodsClassifyCommissionMapper.selectByClazzIDList(map);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        for (ClazzValDto clazzValDto : clazzValDtoList) {
            resultMap.put(clazzValDto.getClazzID(), clazzValDto);
        }
        return resultMap;
    }

    /**
     * 查询扣点值通过分类ID集合
     *
     * @param clazzIDs
     * @return
     */
    public Object selectByClazzIDListV2(List<String> clazzIDs,Long sellerID) {
        // SystemContext.getCompanyID()
        Map<String, Object> map = Maps.newHashMap();
        map.put("list", clazzIDs);
        map.put("sellerID",sellerID);//通过分类ID查询不需要公司ID，云管控传递，也获取不到
        List<GoodsClassifyRebate> clazzValDtoList =
                rebateMapper.selectByClazzIDList(map);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        for (GoodsClassifyRebate clazzValDto : clazzValDtoList) {
            resultMap.put(clazzValDto.getClazzID(), clazzValDto);
        }
        return resultMap;
    }

    /**
     * 查询扣点值通过分类ID集合
     *
     * @param clazzIDs
     * @return
     */
    public Object selectByClazzIDListCopy(List<String> clazzIDs) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("list", clazzIDs);
        //map.put("companyID",SystemContext.getCompanyID());
        map.put("companyID", null);//通过分类ID查询不需要公司ID，云管控传递，也获取不到
        List<ClazzValDto> clazzValDtoList =
                goodsClassifyCommissionMapper.selectByClazzIDList(map);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        for (ClazzValDto clazzValDto : clazzValDtoList) {
            resultMap.put(clazzValDto.getClazzID(), clazzValDto);
        }
        return resultMap;
    }

    /**
     * 操作商品分类-图片
     *
     * @param clazzImageVo
     * @return
     */
    public Object operateClazzImage(ClazzImageVo clazzImageVo) {
        ClazzImageDto clazzImageDto = new ClazzImageDto();
        BeanUtils.copyProperties(clazzImageVo, clazzImageDto);
        //判断分类是否存在
        if (goodsClassifyCommissionMapper.selectByClazzID(clazzImageDto.getClazzID(), SystemContext.getCompanyID()) == null) {
            //新增记录
            clazzImageDto.setID(IdGen.get().nextId());
            clazzImageDto.setCompanyID(SystemContext.getCompanyID());
            return goodsClassifyCommissionMapper.insertImage(clazzImageDto);
        } else {
            //修改记录
            return goodsClassifyCommissionMapper.updateImage(clazzImageDto);
        }
    }

    /**
     * 查询分类是否有专题
     *
     * @param subjectID
     * @return
     */
    public Object queryClazzSubject(Long subjectID) {
        return goodsClassifyCommissionMapper.selectBySubjectID(subjectID, SystemContext.getCompanyID());
    }

    /**
     * 更新专题图片信息
     *
     * @param clazzImageVo
     * @return
     */
    public Object updateBySubjectID(ClazzImageVo clazzImageVo) {
        ClazzImageDto clazzImageDto = new ClazzImageDto();
        BeanUtils.copyProperties(clazzImageVo, clazzImageDto);
        return goodsClassifyCommissionMapper.updateBySubjectID(clazzImageDto);
    }


    /**
     * 更新label
     *
     * @param list
     * @return
     */
    public Integer updateDiscountRateList(List<ClazzRateVo> list) {
        Integer result = goodsClassifyCommissionMapper.updateByDiscountRateList(list);
        return result;
    }


    public Object deleteByCompanyID(Long companyID) {
        return goodsClassifyCommissionMapper.deleteByCompanyID(companyID);
    }


}
